package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/7.<br><br>
 *
 * 买卖股票的最佳时机 II
 */
public class Topic122 {
    public static void main(String[] args) {
        Topic122 instance = new Topic122();

        int[] array = new int[] {7,1,5,3,6,4};
        System.out.println(instance.maxProfit(array));

        array = new int[] {1,2,3,4,5};
        System.out.println(instance.maxProfit(array));

        array = new int[] {7,6,4,3,1};
        System.out.println(instance.maxProfit(array));
    }

    public int maxProfit(int[] prices) {
        if(prices == null || prices.length < 2) {
            return 0;
        }

        int profit = 0;
        for(int i = 0; i < prices.length - 1; i++) {
            if(prices[i + 1] > prices[i]) {
                profit += prices[i + 1] - prices[i];
            }
        }

        return profit;
    }
}
